package com.cleaningcloud.auth.controller;

import cn.hutool.core.util.RandomUtil;

import cn.hutool.json.JSONObject;
import com.cleaningcloud.auth.model.WeiXinSet;
import com.cleaningcloud.auth.service.AliyunSmsService;
import com.cleaningcloud.auth.service.MallLoginService;
import com.cleaningcloud.auth.service.MiniLoginService;
import com.cleaningcloud.cld.api.RemoteStaffService;
import com.cleaningcloud.cld.api.model.LoginStaff;
import com.cleaningcloud.cld.api.model.RegisterStaff;
import com.cleaningcloud.common.core.domain.R;
import com.cleaningcloud.common.core.web.domain.AjaxResult;
import com.cleaningcloud.common.security.service.MallTokenService;
import com.cleaningcloud.common.security.service.MiniTokenService;
import com.cleaningcloud.erp.api.model.LoginCustomer;
import com.cleaningcloud.erp.api.model.RegisterCustomer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 小程序token 控制
 *
 * @author
 */
@RestController
public class MiniTokenController {

    @Autowired
    private MiniTokenService miniTokenService;

    @Autowired
    private MiniLoginService miniLoginService;

    @Autowired
    private MallLoginService mallLoginService;

    @Autowired
    private AliyunSmsService smsService;

    @Autowired
    private MallTokenService mallTokenService;

    @Autowired
    private RemoteStaffService remoteStaffService;

    /**
     * @className MiniTokenController.java
     * @description 云管理(小程序)登录接口
     * @author Shaw
     * @date: 2025-03-31 16:35
     * @param: //TODO
     */
    @PostMapping("miniLogin")
    public R<?> login(String staffPhone, String password)
    {
        //获取微信用户openid
//        StringBuilder url = new StringBuilder("https://api.weixin.qq.com/sns/jscode2session?");
//        url.append("appid=");//appid设置
//        url.append(WeiXinSet.APPID.getSet());
//        url.append("&secret=");//secret设置
//        url.append(WeiXinSet.SECRET.getSet());
//        url.append("&js_code=");//code设置
//        url.append(code);
//        url.append("&grant_type=authorization_code");
//        Map<String, Object> map = null;
//        JSONObject json = miniLoginService.getHttpUrlResult(url);

        // 用户登录
        LoginStaff staffInfo = miniLoginService.login(staffPhone, password);

        // 获取登录token
        return R.ok(miniTokenService.createToken(staffInfo));
    }

    /**
     * @className MiniTokenController.java
     * @description 云管理(小程序)注册接口
     * @author Shaw
     * @date: 2025-03-31 16:35
     * @param: //TODO
     */
    @PostMapping("miniRegister")
    public R<?> register(@RequestBody RegisterStaff registerStaff)
    {
        // 员工注册
        miniLoginService.register(registerStaff);
        return R.ok();
    }

    @GetMapping("smsCode")
    public AjaxResult smsCode(String phone) throws Exception {
        return AjaxResult.success(ResponseEntity.ok(smsService.sendCode(phone, RandomUtil.randomNumbers(4))));
    }

    /**
     * @className MiniTokenController.java
     * @description 三才环科商城(小程序)登录接口
     * @author Shaw
     * @date: 2025-03-31 16:34
     * @param: //TODO
     */
    @PostMapping("mallLogin")
    public R<?> mallLogin(String userName, String password)
    {
        // 用户登录
        LoginCustomer customerInfo = mallLoginService.login(userName, password);
        // 获取登录token
        return R.ok(mallTokenService.createToken(customerInfo));
    }

    /**
     * @className MiniTokenController.java
     * @description 三才环科商城(小程序)注册接口
     * @author Shaw
     * @date: 2025-03-31 16:35
     * @param: //TODO
     */
    @PostMapping("mallRegister")
    public R<?> mallRegister(@RequestBody RegisterCustomer registerCustomer)
    {
        // 用户注册
        mallLoginService.register(registerCustomer);
        return R.ok();
    }
}
